@file:OptIn(ExperimentalFoundationApi::class)

package toy.keli.edic.ui.main.components

import android.content.Intent
import android.net.Uri
import androidx.collection.arraySetOf
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.border
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import toy.keli.edic.data.config.BookConf
import toy.keli.edic.data.config.MenuConf
import toy.keli.edic.data.config.SettingConf
import toy.keli.edic.data.model.Book
import toy.keli.edic.ui.history.HistoryScreen
import toy.keli.edic.ui.history.HistoryScreenEvent
import toy.keli.edic.ui.home.HomeEvents
import toy.keli.edic.ui.main.SettingScreen
import toy.keli.edic.ui.main.components.Display.mt
import toy.keli.edic.ui.navigation.NavScreen
import toy.keli.edic.util.ActivityRun
import toy.keli.edic.util.FileUtil
import java.io.File

@Composable
@Preview
fun BottomNavigationBar(
    navController: NavController?=null,
    itemClick: (BottomNavItem) -> Unit={},
) {
    if( LocalInspectionMode.current) {
        ActivityRun.context = LocalContext.current
    }
    Card(
            modifier = Modifier
                .fillMaxWidth(),
//            elevation = CardDefaults.cardElevation(8.dp),
            shape = RoundedCornerShape(0.dp)
    ) {
        val items = provideBottomNavItems()
        val backStackEntry = navController?.currentBackStackEntryAsState()
        var nowRoute = "Home"
        if(backStackEntry!=null && backStackEntry.value!=null) {
             nowRoute = backStackEntry.value?.destination?.route.toString()
        }
        //backStackEntry.destination.route
        when(MenuConf.type().value) {
            MenuConf.bottom -> {
                val nowRouteName = nowRoute.split("?").first()
                if (nowRouteName in NavScreen.simpleRouteTitles) {
                    if((nowRouteName == "Home" && !HomeEvents.downMenuOpen)) return@Card;
                    Row(
                        modifier = Modifier
                            .border(1.dp, MaterialTheme.colorScheme.background)
                            .padding(8.dp)
                            .fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceAround,
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        val smr = NavScreen.simpleRouteTitles
                        val typeSet = arraySetOf<String>()
                        items.filter {
                            val type = it.route.split("?")[0]
                            val rt = type in smr && !typeSet.contains(type) && it.title.length > 0
                            typeSet.add(type)
                            rt
                        }.forEach { item ->
                            val itemName = item.route.split("?").first()
                            val selected = item.route.startsWith(nowRouteName)
                            item.title = mt(item.title)
                            val menu = smr.get(itemName)

                            NavItem(
                                item = item,
                                onClick = {
                                    //当前条目已经选中并且有子条目的时候
                                    if (selected && menu?.items?.size!! > 0) {
                                        menu?.show { k, v ->
                                            when (itemName) {
                                                NavScreen.routes.Home -> HomeEvents.onChangeViewModel(
                                                    v.name
                                                )

                                                NavScreen.routes.History -> {
                                                    HistoryScreenEvent.onTypeChange(
                                                        v.value.toString().toInt(), v.title
                                                    )
//                                                    NavScreen.HistoryScreen.open("?type="+v.value)
                                                }
                                            }
                                        }
                                    } else {
                                        itemClick(item)
                                        if (itemName == "Home") {
                                            NavScreen.openHome(0)
//                                            HomeEvents.onBookShow(
//                                                Book.cid(),
//                                                articleId = BookConf.instance.id
//                                            )
                                        } else {
                                            HomeEvents.downMenuOpen = true
                                            HomeEvents.timeOutClosedDownMenu = false
                                        }
                                    }
                                },
                                selected = selected,
                                modifier = Modifier
                            )
                            menu?.add()
                        }
                    }
                }
            }
            MenuConf.right ->
                LazyColumn (
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(1.dp)
                        .border(
                            1.dp,// 边框粗细1dp
                            Color.White,// 线性渲染
                            RoundedCornerShape(10.dp)// 形状带圆角
                        )
                        .alpha(0.5f)
                    //modifier = Modifier.padding(8.dp)
                    //horizontalArrangement = Arrangement.SpaceAround,
                    //verticalAlignment = Alignment.CenterVertically
                ) {
                    itemsIndexed(items) { index,item ->
                        val selected = nowRoute?.startsWith(item.route)
                        item.title = mt(item.title)
                        if (selected != null) {
                            NavItem(
                                item = item,
                                onClick = { itemClick(item) },
                                selected = selected,
                                modifier = Modifier
                                    .padding(0.dp, 10.dp)
                                    .width(180.dp)
                                    .border(2.dp, Color.Gray, RoundedCornerShape(12.dp)),hasIco = true
                            )
                        }
                    }
                }
        }
    }
}